package springdao.model;

import java.util.HashSet;
import java.util.Set;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.ForeignKeyAction;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Queries;
import javax.jdo.annotations.Query;

/**
 *
 * @author Kent Yeh
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, table = "member", detachable = "true")
@FetchGroup(name = "lazy", members =@Persistent(name = "userstores"))
@Queries({@Query(name="Member.findByName",value="SELECT FROM springdao.model.Member WHERE name.matches(param) PARAMETERS String  param"),
    @Query(name="Member.findNativeByName",language="javax.jdo.query.SQL",value="SELECT * FROM member WHERE name like ?")})
public class Member {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
    private Long id;
    @Persistent
    @Column(jdbcType = "VARCHAR", length = 20)
    private String name;
    @Persistent
    @Column(jdbcType = "VARCHAR", length = 1, allowsNull = "false", name = "userType")
    private SupplyChainMember userType = SupplyChainMember.C;
    @Persistent(mappedBy = "owner",defaultFetchGroup="true",dependentElement="true")
    @Element(deleteAction= ForeignKeyAction.CASCADE,dependent="true")
    private Set<Phone> phones = new HashSet();
    @Persistent(mappedBy = "owner")
    @Element
    private Set<UserStore> userstores = new HashSet();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SupplyChainMember getUserType() {
        return userType;
    }

    public void setUserType(SupplyChainMember userType) {
        this.userType = userType;
    }

    public Set<Phone> getPhones() {
        return phones;
    }

    public void setPhones(Set<Phone> phones) {
        this.phones = phones;
    }

    public Set<UserStore> getUserstores() {
        return userstores;
    }

    public void setUserstores(Set<UserStore> userstores) {
        this.userstores = userstores;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Member other = (Member) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 71 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return new StringBuilder(name).append("@").append(userType.toString()).toString();
    }
}
